<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_spc extends CI_Model {

	public function __construct() 
	{
		parent::__construct();
		
		$this->load->helper(array('form','captcha','array','download'));
		$this->load->library(array('form_validation','session','table','sam','tank_auth','upload'));
		$this->load->database();
		//$this->model_auth->_must_logged_in('spc');
	}
	
	public function _get_all () {
		$this->db->order_by("spc_no_tim", "asc");
		$query = $this->db->get('spc');
		$data = $query->result();
		return $data;	
	}	
	
	
	public function _get_valid () {
		$this->db->order_by("spc_no_tim", "asc");
		$query = $this->db->get_where('spc',array('spc_valid' => 1));
		$data =$query->result();
		return $data;	
	}

	public function _get_not_valid () {
		$this->db->order_by("spc_no_tim", "asc");
		$query = $this->db->get_where('spc',array('spc_valid' => 0));
		$data =$query->result();
		return $data;	
	}
	
	public function _get_finalis () {
		$this->db->order_by("spc_no_tim", "asc");
		$query = $this->db->get_where('spc',array('spc_finalis' => 1));
		$data = $query->result();
		return $data;	
	}
	
	public function _get_userdata ($num='default') {
		$query = $this->db->get_where('spc',array('spc_no_tim' => (($num=='default')?$this->session->userdata('usertype_id'):$num)));
		$data = $query->row();
		return $data;	
	}
	
	public function _edit_kontestan ($data, $usertype_id='') {
		if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
		$select= array(	'spc_nama_tim',
						'spc_nama_pt',
						'spc_alamat_pt',
						'spc_nama_1',
						'spc_nohp',
						'spc_email',
						'spc_nama_2',
						'spc_nama_3',
						'spc_pembimbing',
						);
		$data= elements($select, $data);

		$this->db->where('spc_no_tim', $usertype_id);
		$this->db->update('spc', $data); 		

	}
	
	public function _insert_scan_data ($data, $usertype_id='') {
		//if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
		$userdata = $this->_get_userdata();
	
		//spc_link_ktm1 	spc_link_ktm2 	spc_link_ktm3 	spc_link_buktibayar
		$update = array(
			'spc_link_ktm1'			=>
				($data['spc_link_ktm1']!='')?$data['spc_link_ktm1']:$userdata->spc_link_ktm1,
			'spc_link_ktm2'			=>
				($data['spc_link_ktm2']!='')?$data['spc_link_ktm2']:$userdata->spc_link_ktm2,		
			'spc_link_ktm3'			=> 
				($data['spc_link_ktm3']!='')?$data['spc_link_ktm3']:$userdata->spc_link_ktm3,		
			'spc_link_buktibayar'	=> 
				($data['spc_link_buktibayar']!='')?$data['spc_link_buktibayar']:$userdata->spc_link_buktibayar,		
		);
			
		//print_r($retval);
		$this->db->where('spc_no_tim', $userdata->spc_no_tim);
		$this->db->update('spc', $update); 		
		
		//return $retval;
	}
	
	public function _insert_kaos_data ($data, $usertype_id='') {
		//if ($usertype_id=='') $usertype_id=$this->session->userdata('usertype_id');
		$userdata = $this->_get_userdata();
	
		//spc_link_ktm1 	spc_link_ktm2 	spc_link_ktm3 	spc_link_buktibayar
		$select = array ('spc_kaos_1',
						'spc_kaos_2',
						'spc_kaos_3');
		
		$data= elements($select, $data);
		//print_r($retval);
		$this->db->where('spc_no_tim', $userdata->spc_no_tim);
		$this->db->update('spc', $data); 		
		
		//return $retval;
	}
	
	public function _create_peserta_kuliah ($data = NULL) {	
		/*
		register	
			dx_auth/user -> function set_user($user_id, $data)
		*/
	
		$select= array(	'spc_nama_tim',
						'spc_nama_pt',
						'spc_alamat_pt',
						'spc_nama_1',
						'spc_nohp',
						'spc_email',
						'spc_nama_2',
						'spc_nama_3',
						'spc_pembimbing',
						'spc_agreement'
						);
		//ambil data dengan index select dari post
		$data= elements($select, $data);
		$query = $this->db->get_where('spc', $data);
		
		
		if ($query->num_rows()==0) {
		// if (1) {
		
			$sql  = $this->db->insert_string('spc',$data);
			$query= $this->db->query($sql);
			
			//tarik dari database cari no reg nya
			$query = $this->db->get_where('spc', $data);
			$no_reg  = $query->row('spc_no_tim');
			$email  = $query->row('spc_email');
			
			//atur username and passwordnya
			$username = sprintf("SPC%03d",$no_reg);
			$password = $this->sam->_random_string(10);
			// $password_encrypted = sha1($password);
			
			$usertype='spc';
			$usertype_id=$no_reg;
			//print_r($data);	
			
			$this->tank_auth->create_user($username, $email, $password,$usertype,$usertype_id, FALSE);
		
			$data2= array (	'username' => $username,
							'def_password' => $password,
							'email' => $email);
			
			$mailparam = (object) $data;
			$mailparam->username=$username;
			$mailparam->password=$password;
			// $mailparam = array ( 'username' => $username,
							// 'def_password' => $password,
							// 'email' => $email,
							// 'userdata' => $data,							
							// );
			
			$this->_sendmail($mailparam);
			$this->session->set_userdata($data2);
			return 1;
		}  else
			return 0;
	}
	
	
	public function _sendmail($data) {
		$this->load->library('email');
		$config['protocol'] = 'sendmail';
		$config['mailtype'] = 'html';
		$this->email->initialize($config);
	
		// print_r($data);
		// $userdata=$data['userdata'];
////////////////////////////////////////////////////		
///////////////      KONTEN EMAIL 
		$email = $data->spc_email;
		$subject = 'Pendaftaran ITB Senior Programming Contest 2011';
		
		// 'spc_nama_tim',
						// 'spc_nama_pt',
						// 'spc_alamat_pt',
						// 'spc_nama_1',
						// 'spc_nohp',
						// 'spc_email',
						// 'spc_nama_2',
						// 'spc_nama_3',
						// 'spc_pembimbing',
						
		// $email_debug=TRUE;
		$email_debug=FALSE;
		$content=
			// ''
			$this->load->view('email/spc_pendaftaran',array('userdata'=>$data),!$email_debug)
			;
		
			// ';
		// $altcontent='';
		
////////////////////////////////////////////////////
////////////////////////////////////////////////////
	
		
		$this->email->from('itbpc@if.itb.ac.id', 'ITB Programming Contest 2011 Commitee');
		$this->email->to($email);
		$this->email->bcc('itbpc.official@gmail.com');
		$this->email->reply_to('itbpc.official@gmail.com', 'ITB Programming Contest 2011 Commitee');

		$this->email->subject($subject);
		$this->email->message($content);
		// $this->email->set_alt_message($altcontent);
		
		$this->email->send();
		if ($email_debug) {echo $this->email->print_debugger();}
	
	}
	
	public function _make_warning ($num='default') {
		
		$data= $this->_get_userdata (($num=='default')?$this->session->userdata('usertype_id'):$num);	
		$retval = array (
			'exist' => ($data->spc_link_ktm1==''
						||($data->spc_link_ktm2==''&&$data->spc_nama_2!='')
						||($data->spc_link_ktm3==''&&$data->spc_nama_3!='')
						||$data->spc_link_buktibayar==''
						||($data->spc_finalis==1&&(
						$data->spc_kaos_1==''||
						($data->spc_kaos_2==''&&$data->spc_nama_2!='')||
						($data->spc_kaos_3==''&&$data->spc_nama_3!='')
						))				
						)?1:0,
			// 'anggota_2'	=>($data->spc_nama_2=='')?1:0,
			// 'anggota_3'	=>($data->spc_nama_3=='')?1:0,
			'scanktm_1'	=>($data->spc_link_ktm1=='')?1:0,
			'scanktm_2'	=>($data->spc_link_ktm2==''&&$data->spc_nama_2!='')?1:0,
			'scanktm_3'	=>($data->spc_link_ktm3==''&&$data->spc_nama_3!='')?1:0,
			'buktibayar'=>($data->spc_link_buktibayar=='')?1:0,
			'kaos'		=>($data->spc_finalis==1&&(
						$data->spc_kaos_1==''||
						($data->spc_kaos_2==''&&$data->spc_nama_2!='')||
						($data->spc_kaos_3==''&&$data->spc_nama_3!='')
						))?1:0,
			);
		return $retval;
	}

}